package com.fanly.weixin.utils;

import cn.hutool.core.util.StrUtil;
import com.fanly.weixin.entity.joke.Joke;
import com.google.gson.Gson;

public class JokeUtils {

//	public static Joke getJoke(){
//		List<Joke> list = new ArrayList<Joke>();
//		Joke joke = new Joke();
//		joke.setTitle("java程序猿的编程之道：\n");
//		joke.setText("1、java语言入门\n2、java编程思想\n3、编程之美\n");
//		list.add(joke);
//		
//		joke = new Joke();
//		joke.setTitle("攻程狮爱情观");
//		joke.setTitle("爱情就是死循环，一旦执行就陷进去了；爱上一个人，就是内存泄漏--你永远无法释放。");
//		list.add(joke);
//		Collections.shuffle(list);
//		return list.get(0);
//	}
	public static String getJoke(){
		String httpUrl = "http://apis.haoservice.com/lifeservice/Joke/ContentList";
		String httpArg = "key=39b706fd004842818be7bf55649c5390&pagesize=1&page="+(1+(int)(Math.random()*44929));  
		String jsonResult  = UrlUtils.getData(httpUrl, httpArg);
		if(StrUtil.isEmpty(jsonResult)){
			return "";
		}else{
			//转换成对应的bean
			Gson gson = new Gson();
			Joke joke = gson.fromJson(jsonResult, Joke.class);
			System.out.println(joke);
			StringBuffer buffer = new StringBuffer();
			if(joke!=null && joke.getError_code()==0 && "成功".equals(joke.getReason())){
				if(joke.getResult()!=null && joke.getResult().size()>0){
					buffer.append(joke.getResult().get(0).getTitle()+": \n        "+joke.getResult().get(0).getContent());
					return buffer.toString();
				}
			}
		}
			
		return "";
		
	}
	public static void main(String[] args) {
		System.out.println(1+(int)(Math.random()*44929));
		System.out.println(getJoke());
	}
}
